;;; four digits (1000 <= n <= 9999), its fifth power range?
;;; five digits (10000 <= n <= 99999), its range?
;;; six digits and seven digits ...

(defun digits (num)
  (map 'list
       (lambda (char) (read-from-string (string char)))
       (prin1-to-string num)))

(defun nth-power (n &key (power 5))
  (reduce '+
          (mapcar (lambda (x) (expt x power))
                  (digits n))))

(defun digit-of-sum-power (&key (lower 2) (upper 354294) (power 5))
  (loop for i from lower to upper
        when (= i (nth-power i :power power))
        collect i))

(defun p3 ()
  (reduce '+ (digit-of-sum-power)))

(defun p3-eg ()
  (reduce '+ digit-of-sum-power :upper 10000 :power 4))

(format t "~a~%" (time (p3)))
(format t "~a~%" (time (p3-eg)))
